package chapter10;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/19 15:27
 */
class Clothes {
    private String id;
    private Size size;

    public Clothes(String id, Size size) {
        this.id = id;
        this.size = size;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Size getSize() {
        return size;
    }

    public void setSize(Size size) {
        this.size = size;
    }
}

public class EnumMapTest {
    public static Map<Size, Integer> countBySize(List<Clothes> clothes) {
        Map<Size, Integer> map = new EnumMap<Size, Integer>(Size.class);
        for (Clothes clothe : clothes) {
            Size size = clothe.getSize();
            map.merge(size, 1, (a, b) -> a + b);
        }

        return map;
    }

    public static void main(String[] args) {
        List<Clothes> clothes = Arrays.asList(new Clothes[]{
                new Clothes("C001", Size.LARGE),
                new Clothes("C002", Size.MEDIUM),
                new Clothes("C003", Size.SMALL),
                new Clothes("C004", Size.MEDIUM),
                new Clothes("C005", Size.LARGE)
        });

        System.out.println(countBySize(clothes));

    }
}
